import re
import skuDic   #加载特殊SKU代码的字典
import globalvar as gl

'''
拆解颜色尺寸之间无分隔符的情况
一种牺牲灵活性的实现方案：枚举所有尺码结尾代码的情况
S,M,L,XL,2XL,2X,3XL,3X,4XL,4X,5XL,5X
'''
def splitSmoothColorSize(code):
	# 存储 颜色代码 和 尺寸代码 的变量
	ColorCode,SizeCode = (0,0)

	# 当结尾是S
	if code[-1]=='S':
		ColorCode = code[:-1]
		SizeCode = 'S'
	elif code[-1]=='M':
		ColorCode = code[:-1]
		SizeCode = 'M'
	elif code[-1]=='X':
		ColorCode = code[:-2]  # 只考虑2X以上的情况
		SizeCode = code[-2:]+'L'  #补足nXL的描述
	elif code[-1]=='L':
		if code[-2]!='X':
			ColorCode = code[:-1]
			SizeCode = 'L'
		else:
			if code[-3].isalpha():  #倒数第3个字符并不是数字
				ColorCode = code[:-2]
				SizeCode = 'XL'
			else:
				ColorCode = code[:-3]
				SizeCode = code[-3:]
	else:
		print('Unknown SizeCode! error_code='+code)
		exit()
	return (ColorCode,SizeCode)
'''  TEST CODE
codes = ['BGS','LYM','GDL','WBGXL','WAG2XL','GR2X','N3XL','BK3X','B4XL','WH4X','W5XL','SL5X']
for codestr in codes:
	print(splitSmoothColorSize(codestr))
'''

'''
各模式范例
 SPUCode.xxx -> (?:[A-Za-z]+\|)*[A-Z\|a-z\d]+\.[A-Za-z]+
 SPUCode-xxx -> (?:[A-Za-z]+\|)*[A-Z\|a-z\d]+-[A-Za-z0-9-]+
 	SPUCode-[Color-Size] -> 方括号内部分：[A-Z]+-[A-Z0-9]+
 	SPUCode-[ColorSize] -> 方括号内部分：[A-Z]+[A-Z0-9]+
 SPUCode_xxx -> (?:[A-Za-z]+\|)*[A-Z\|a-z\d]+_[A-Za-z0-9-_]+
 	SPUCode_[Color_Size]  ->  方括号内部分：[A-Z]+_[A-Z0-9]+
 	SPUCode_[ColorSize]  ->  方括号内部分：见上文
'''

## 拆解颜色与尺寸无缝连接的SKU代码,返回值是元组:(SPUCode, ColorCode, SizeCode)
def parseColorSizeCode(wholeSKUCode):
	wholeSKUCode = str(wholeSKUCode)
	#print(wholeSKUCode)
	## 点式分隔代码模式
	dotpat = re.compile(r'(?:[A-Za-z0-9]+\|)*([A-Za-z\d]+)\.([A-Za-z0-9]+)')
	## 横杠式分隔代码模式
	hbarpat = re.compile(r'(?:[A-Za-z0-9]+\|)*([A-Za-z\d]+)-([A-Za-z0-9-]+)')
	## 下划线分隔代码模式
	bottomlinepat = re.compile(r'(?:[A-Za-z0-9]+\|)*([A-Za-z\d]+)_([A-Za-z0-9-_]+)')

	## 下划线分隔颜色尺码模式
	bottomlinepatCS = re.compile(r'([A-Z]+)_([A-Z0-9]+)')
	## 横杠分隔颜色尺码模式
	hbarpatCS = re.compile(r'([A-Z]+)-([A-Z0-9]+)')
	## 无分隔颜色尺码模式
	smoothpatCS = re.compile(r'([A-Z]+[A-Z0-9]+)')

	## 定义存储 SPU,颜色,尺码 的变量
	LocalSKU,SPUCode,ColorCode,SizeCode = (0,0,0,0)

	sku_dic = gl.get_value('special_sku_dic')
	skuc = sku_dic.get(wholeSKUCode)
	if skuc!=None:
		(LocalSKU,SPUCode,ColorCode,SizeCode) = skuc
	else:
		rs = dotpat.match(wholeSKUCode)
		if rs!=None:
			SPUCode = rs.group(1)
			colorSizeCode = rs.group(2)
			LocalSKU = SPUCode+'.'+colorSizeCode
			#print('SPU='+SPUCode)
			rs2 = bottomlinepatCS.match(colorSizeCode)
			if rs2!=None:
				ColorCode = rs2.group(1)
				SizeCode = rs2.group(2)
			else:
				rs2 = hbarpatCS.match(colorSizeCode)
				if rs2!=None:
					ColorCode = rs2.group(1)
					SizeCode = rs2.group(2)
				else:
					rs2 = smoothpatCS.match(colorSizeCode)
					if rs2!=None:
						#print('SPU='+SPUCode+',ColorSize='+colorSizeCode)
						ColorCode,SizeCode = splitSmoothColorSize(colorSizeCode)
		else:
			rs = hbarpat.match(wholeSKUCode)
			if rs!=None:
				SPUCode = rs.group(1)
				colorSizeCode = rs.group(2)
				LocalSKU = SPUCode+'-'+colorSizeCode
				#print('SPU='+SPUCode)
				rs2 = bottomlinepatCS.match(colorSizeCode)
				if rs2!=None:
					ColorCode = rs2.group(1)
					SizeCode = rs2.group(2)
				else:
					rs2 = hbarpatCS.match(colorSizeCode)
					if rs2!=None:
						ColorCode = rs2.group(1)
						SizeCode = rs2.group(2)
					else:
						rs2 = smoothpatCS.match(colorSizeCode)
						if rs2!=None:
							ColorCode,SizeCode = splitSmoothColorSize(colorSizeCode)
				#print('color='+ColorCode+',size='+SizeCode)
			else:
				rs = bottomlinepat.match(wholeSKUCode)
				if rs!=None:
					SPUCode = rs.group(1)
					colorSizeCode = rs.group(2)
					LocalSKU = SPUCode+'_'+colorSizeCode
					#print('SPU='+SPUCode)
					rs2 = bottomlinepatCS.match(colorSizeCode)
					if rs2!=None:
						ColorCode = rs2.group(1)
						SizeCode = rs2.group(2)
					else:
						rs2 = hbarpatCS.match(colorSizeCode)
						if rs2!=None:
							ColorCode = rs2.group(1)
							SizeCode = rs2.group(2)
						else:
							rs2 = smoothpatCS.match(colorSizeCode)
							if rs2!=None:
								ColorCode,SizeCode = splitSmoothColorSize(colorSizeCode)
					#print('color='+ColorCode+',size='+SizeCode)
				else:
					print(f"I do not know what pattern of this SKU is! val={wholeSKUCode}")
		return (LocalSKU,SPUCode,ColorCode,SizeCode)

# == 解析图片文件名，从中拆解出spu、color code、order number ==
# 图片文件名的例外情况
def specialImage(name):
	pat = re.compile(r'X110965.G1[\w\d\.]*')
	rs = pat.match(name)
	ret, spu, color, order = (False, '', '', -1)
	if rs!=None:
		ret = True
		spu = 'X110965'
		color = 'G1'
		pat2 = re.compile(r'-(\d)-3x4')
		rs2 = pat2.match(name)
		if rs2!=None:
			order = rs2.group(1)
	return ret, spu, color, order


# name 图片文件名,格式形如: CN276T_BK-1.jpg、CN276T_BK-100X100.jpg、X116228.BK-3.jpeg、X116228.BK-100X100.jpg
def parseImageFileName(full_name):
	# 先判断是不是特殊情况
	is_special, spu, color, order = specialImage(full_name)
	if not is_special:
		#先去掉图床路径中的文件夹名称
		name = full_name
		if full_name.rfind('/'):
			name = full_name.split('/')[-1]
		
		# 先用“-”拆分
		segs = name.split('-')
		spucolor = segs[0]
		order_or_size = segs[1]
		size = (order_or_size.split('.'))[0]
		if size=='3x4':
			print(f'[parseSKUCode.py::parseImageFileName]Image File Name Format Error. Name:{name}')
		elif size=='100x100':
			order = -1
		else:
			order = size

		seperator = ''
		if spucolor.find('.')!=-1:
			seperator = '.'
		elif spucolor.find('_')!=-1:
			seperator = '_'
		else:
			print(f'[parseSKUCode.py::parseImageFileName]unknown seperator:{name}')
			exit()
		spu_color_list = spucolor.split(seperator)
		spu = spu_color_list[0]
		color = spu_color_list[1]
	return spu, color, order
'''
# 测试
skus = ['CN276T_BK-1.jpg', 'CN276T_BK-100X100.jpg', 'X116228.BK-3.jpeg', 'X116228.BK-100X100.jpg']
for sku in skus:
	print(parseImageFileName(sku))
'''